/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Net;
using System.Net.Sockets;

namespace OpenServers.Net.Common.Socket {
    /// <summary>
    /// A TCP socket.
    /// </summary>
    public class TcpSocket : ISocket {
        private readonly global::System.Net.Sockets.Socket _socket;

        /// <summary>
        /// constructor. Initializes underlying socket.
        /// </summary>
        public TcpSocket() {
            _socket = new global::System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        ///<summary>
        /// constructor. Sets the underlying socket to the given socket.
        ///</summary>
        public TcpSocket(global::System.Net.Sockets.Socket socket) {
            _socket = socket;
        }

        /// <summary>
        /// perform async send.
        /// </summary>
        public void BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state) {
            _socket.BeginReceive(buffer, offset, size, socketFlags, callback, state);
        }

        /// <summary>
        /// perform async receive.
        /// </summary>
        public int EndReceive(IAsyncResult ar, out SocketError socketError) {
            return _socket.EndReceive(ar, out socketError);
        }

        /// <summary>
        /// begin async disconnect.
        /// </summary>
        public void BeginDisconnect(bool reuseSocket, AsyncCallback callback, object state) {
            _socket.BeginDisconnect(reuseSocket, callback, state);
        }

        /// <summary>
        /// close the socket.
        /// </summary>
        public void Close() {
            _socket.Close();
        }

        /// <summary>
        /// begin async send.
        /// </summary>
        public void BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state) {
            _socket.BeginSend(buffer, offset, size, socketFlags, callback, state);
        }

        /// <summary>
        /// end async send.
        /// </summary>
        public int EndSend(IAsyncResult ar, out SocketError socketError) {
            return _socket.EndSend(ar, out socketError);
        }

        /// <summary>
        /// gets the local address of this socket.
        /// </summary>
        public IPAddress LocalAddress {
            get { return ((IPEndPoint)_socket.LocalEndPoint).Address; }
        }

        /// <summary>
        /// gets the local port of this socket.
        /// </summary>
        public int LocalPort {
            get { return ((IPEndPoint)_socket.LocalEndPoint).Port; }
        }

        /// <summary>
        /// gets the remote address of this socket.
        /// </summary>
        public IPAddress RemoteAddress {
            get { return ((IPEndPoint)_socket.RemoteEndPoint).Address; }
        }

        /// <summary>
        /// gets the remote port of this socket.
        /// </summary>
        public int RemotePort {
            get { return ((IPEndPoint)_socket.RemoteEndPoint).Port; }
        }

        /// <summary>
        /// bind this socket to a local address and port.
        /// </summary>
        public void Bind(IPAddress address, int port) {
            _socket.Bind(new IPEndPoint(address, port));
        }

        /// <summary>
        /// begin an async connection.
        /// </summary>
        public void BeginConnect(IPAddress address, int port, AsyncCallback callback, object state) {
            _socket.BeginConnect(address, port, callback, state);
        }

        /// <summary>
        /// end async connect.
        /// </summary>
        public void EndConnect(IAsyncResult ar) {
            _socket.EndConnect(ar);
        }

        /// <summary>
        /// end async disconnect.
        /// </summary>
        public void EndDisconnect(IAsyncResult ar) {
            _socket.EndDisconnect(ar);
        }
    }
}
